package com.zhangwei.day38.fragment;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.zhangwei.day38.R;

import java.io.File;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.zhangwei.day38.fragment
 * @Description:
 * @date 15/10/20 上午9:35
 */

public class HttpFragment extends Fragment {

    private HttpHandler<String> handler;
    @ViewInject(R.id.progress)
    private ProgressBar progressBar;
    @ViewInject(R.id.down)
    private Button down;
    private HttpHandler<File> download;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_http, container, false);
        ViewUtils.inject(this, view);
        initView();
        downloadMethod();
        return view;
    }

    private void initView() {

    }

    /**
     * 加载数据
     */
    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        handler = httpUtils.send(HttpRequest.HttpMethod.GET, "", new RequestCallBack<String>() {

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);

            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String reuslt = responseInfo.result;
            }


            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });

    }

    /**
     * post方式加载数据
     */
    private void postMethod() {
        HttpUtils httpUtils = new HttpUtils();

        httpUtils.send(HttpRequest.HttpMethod.POST, "", getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });


    }

    /**
     * 请求参数
     *
     * @return
     */
    private RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("id", "");
        params.addBodyParameter("name", "name");
        params.addBodyParameter("key", new File(""));


        return params;
    }

    /**
     * 文件下载
     *
     *
     * HttpRequest.HttpMethod method, 请求方式
     * String url,下载url
     * String target,下载文件的路径
     * RequestParams params,  上传参数
     * boolean autoResume, 是否断点续传
     * boolean autoRename,  是否自动命名
     *
     * RequestCallBack<File> callback 回调接口
     */
    private void downloadMethod() {
        HttpUtils httpUtils = new HttpUtils();
        download = httpUtils.download(HttpRequest.HttpMethod.GET, "http://file-bak.liqucn.com/upload/2015/shejiao/com.tencent.mm_6.3.5.49_liqucn.com.apk", Environment.getExternalStorageDirectory() + File.separator + "13213131313.apk", null, true, false, new RequestCallBack<File>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
                progressBar.setMax((int) total);
                progressBar.setProgress((int) current);
            }

            @Override
            public void onSuccess(ResponseInfo<File> responseInfo) {

            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    @OnClick(R.id.down)
    public void onClick(View view) {
        if (download.isPaused()) {
            download.resume();
        } else {
            download.pause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
//        handler.cancel();
    }


}
